package org.jeecg.modules.santu.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.santu.entity.TbAdvertising;
import org.jeecg.modules.santu.service.ITbAdvertisingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 广告管理
 * @Author: jeecg-boot
 * @Date:   2025-04-16
 * @Version: V1.0
 */
@Api(tags="广告管理")
@RestController
@RequestMapping("/santu/tbAdvertising")
@Slf4j
public class TbAdvertisingController extends JeecgController<TbAdvertising, ITbAdvertisingService> {
	@Autowired
	private ITbAdvertisingService tbAdvertisingService;
	
	/**
	 * 分页列表查询
	 *
	 * @param tbAdvertising
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "广告管理-分页列表查询")
	@ApiOperation(value="广告管理-分页列表查询", notes="广告管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(TbAdvertising tbAdvertising,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<TbAdvertising> queryWrapper = QueryGenerator.initQueryWrapper(tbAdvertising, req.getParameterMap());
		Page<TbAdvertising> page = new Page<TbAdvertising>(pageNo, pageSize);
		IPage<TbAdvertising> pageList = tbAdvertisingService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @GetMapping(value = "/enable")
	 public Result<?> enable(@RequestParam(name="id",required=true) String id) {
		 TbAdvertising byId = tbAdvertisingService.getById(id);
		 byId.setAdvertisingStatus(TbAdvertising.enable);
		 tbAdvertisingService.updateById(byId);
		 return Result.OK("开启成功!");
	 }

	 @GetMapping(value = "/unenable")
	 public Result<?> unenable(@RequestParam(name="id",required=true) String id) {
		 TbAdvertising byId = tbAdvertisingService.getById(id);
		 byId.setAdvertisingStatus(TbAdvertising.unenable);
		 tbAdvertisingService.updateById(byId);
		 return Result.OK("关闭成功!");
	 }

	/**
	 *   添加
	 *
	 * @param tbAdvertising
	 * @return
	 */
	@AutoLog(value = "广告管理-添加")
	@ApiOperation(value="广告管理-添加", notes="广告管理-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody TbAdvertising tbAdvertising) {

		int count = tbAdvertisingService.count();
		System.out.println("count------->"+count);
		if(count>=1){
			return Result.error("添加失败，已经有一条了！");
		}
		tbAdvertisingService.save(tbAdvertising);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param tbAdvertising
	 * @return
	 */
	@AutoLog(value = "广告管理-编辑")
	@ApiOperation(value="广告管理-编辑", notes="广告管理-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody TbAdvertising tbAdvertising) {
		tbAdvertisingService.updateById(tbAdvertising);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "广告管理-通过id删除")
	@ApiOperation(value="广告管理-通过id删除", notes="广告管理-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		tbAdvertisingService.removeById(id);
		return Result.OK("删除成功!");
	}

	 @GetMapping("enablejilishipinAdvertising")
	 public Result<?> enableAdvertising(){
		 QueryWrapper<TbAdvertising> tbAdvertisingQueryWrapper = new QueryWrapper<>();
		 tbAdvertisingQueryWrapper.lambda().eq(TbAdvertising::getDescs,"激励视频广告");
		 TbAdvertising one = tbAdvertisingService.getOne(tbAdvertisingQueryWrapper);
		 if(one!=null){
			 Integer advertisingStatus = one.getAdvertisingStatus();
			 if(advertisingStatus==TbAdvertising.enable){
			 	//开启
				 return Result.OK("success!");
			 }else{
			 	//关闭
				 return Result.OK("failure!");
			 }
		 }else{
			 return Result.error("failure");
		 }
	 }

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "广告管理-批量删除")
	@ApiOperation(value="广告管理-批量删除", notes="广告管理-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.tbAdvertisingService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "广告管理-通过id查询")
	@ApiOperation(value="广告管理-通过id查询", notes="广告管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		TbAdvertising tbAdvertising = tbAdvertisingService.getById(id);
		if(tbAdvertising==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(tbAdvertising);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param tbAdvertising
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TbAdvertising tbAdvertising) {
        return super.exportXls(request, tbAdvertising, TbAdvertising.class, "广告管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TbAdvertising.class);
    }

}
